//
//  PopoverAnimator.swift
//  weibo-new
//
//  Created by mac on 2017/3/27.
//  Copyright © 2017年 gjx. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject, UIViewControllerTransitioningDelegate {
    
    fileprivate let duration = 0.5 // 动画时间
    fileprivate var isPresent = false  // 是否是present动画
    var presentedFrame: CGRect = CGRect.zero
    
    var callback: ((Bool) -> Void) // 回调闭包
    
    
    // 自定义析构函数
    init(callBack: @escaping (_ present: Bool) -> Void) {
        self.callback = callBack
    }
    
    // 修改presentedView尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let popoverVC = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        popoverVC.presentedFrame = presentedFrame
        return popoverVC
    }
    
    // 自定义present动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        callback(isPresent)
        return self
    }
    
    // 自定义dismiss动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}

extension PopoverAnimator: UIViewControllerAnimatedTransitioning {
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if isPresent {
            animationForPresent(using: transitionContext)
        }
        else {
            animationForDismiss(using: transitionContext)
        }
    }
    
    /// present动画
    fileprivate func animationForPresent(using transitionContext: UIViewControllerContextTransitioning) {
        // 1. 设置presentedView形变属性
        guard let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        presentedView.transform = CGAffineTransform(scaleX: 1, y: 0.0001)
        
        //2. 添加presentedView到containerView
        transitionContext.containerView.addSubview(presentedView)
        
        // 2.1 设置锚点
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        // 3. 执行动画
        UIView.animate(withDuration: duration, animations: {
            presentedView.transform = CGAffineTransform.identity
        }) {(finished: Bool) in
            transitionContext.completeTransition(finished)
        }
    }
    
    /// dismiss动画
    fileprivate func animationForDismiss(using transitionContext: UIViewControllerContextTransitioning) {
        // 1. 获得消失的view
        guard let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        
        // 执行动画
        UIView.animate(withDuration: duration, animations: { 
            dismissView.transform = CGAffineTransform(scaleX: 1, y: 0.0001)
        }) {[weak self] (finished: Bool) in
            dismissView.removeFromSuperview()
            self?.callback((self?.isPresent)!)
            transitionContext.completeTransition(finished)
        }
    }
    
}
